package m.jclecai.com.activity;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.DebugBean;
import m.jclecai.com.utils.PreferenceUtil;

/**
 * Created by ZD on 2017/7/10.
 */

public class DebugActivity extends BaseActivity {
    @BindView(R.id.txtPassword)
    EditText txtPassword;
    @BindView(R.id.txtVersion)
    TextView txtVersion;
    @BindView(R.id.http_url)
    Spinner http_url;
    @BindView(R.id.param)
    Spinner mParam;

    DebugBean mBean;

    @Override
    protected int getContentView() {
        return R.layout.activity_debug;
    }

    @Override
    protected void initView() {
        super.initView();
        leftOptionText("");
        setTitle("DEBUG");
        mBean = PreferenceUtil.getDebugBean(this);
        if(mBean == null)
            mBean = new DebugBean();
        String[] http = getResources().getStringArray(R.array.http_type);
        int i = Arrays.asList(http).indexOf(mBean.getHttpUrl());
        http_url.setSelection(i);
        String[] param = getResources().getStringArray(R.array.param_type);
        int pIndex = Arrays.asList(param).indexOf(mBean.getParam());
        mParam.setSelection(pIndex);
        getAppVersionName();
    }

    @OnClick(R.id.btnConfirm)
    void sure(){
//        String password = txtPassword.getText().toString();
//        if(TextUtils.isEmpty(password)){
//            DisPlay("请输入密码");
//            return;
//        }
//        if(!password.equals(txtVersion.getText().toString())){
//            DisPlay("密码输入有误");
//            return;
//        }
        mBean.setHttpUrl(http_url.getSelectedItem().toString());
        mBean.setParam(mParam.getSelectedItem().toString());
        PreferenceUtil.setDebugBean(this,mBean);
        DisPlay("保存成功!");
    }

    /**
     * 返回当前程序版本名
     */
    public String getAppVersionName() {
        String versionName = "1.0.0";
        txtVersion.setText(versionName);
        try {
            // ---get the package info---
            PackageManager pm = getPackageManager();
            PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);
            txtVersion.setText("");
            txtVersion.append(pi.versionName);
            txtVersion.append("_");
            txtVersion.append(String.valueOf(pi.versionCode));
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return versionName;
    }
}
